package com.colorlight.cloud.gateway.controlle;

import com.colorlight.cloud.gateway.lock.LockObject;
import com.colorlight.cloud.gateway.service.CommandService;
import com.colorlight.cloud.gateway.service.PublishStrategy;
import com.colorlight.cloud.gateway.service.impl.*;
import com.colorlight.cloud.xm.entity.instructions.Encrypt;
import com.colorlight.cloud.xm.entity.instructions.SetTimeZone;
import com.colorlight.cloud.xm.entity.po.*;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.*;
import java.util.*;

/**
 * @author Demon 消息下发
 */
@Slf4j
@RestController
public class CommandController {
	
	private final List<PublishStrategy> publishStrategyList = new ArrayList<>(3);
	private final CommandService commandService;
	
	public CommandController(CommandService commandService) {
		this.commandService = commandService;
		publishStrategyList.add(new GrandShow());
		publishStrategyList.add(new Xm());
		publishStrategyList.add(new Complete());
	}
	
	@PostMapping("/client/command/publish")
	public void Acu35PublishCommands(@RequestParam String json,
									 @RequestParam String deviceName,
									 @RequestParam String type,
									 @RequestParam String productKey,
									 @RequestParam String uuid,
									 @RequestParam Long seqId) {
		Long publish = null;
		seqId = null;
		// fixme XM与 GrandSHow做特殊化处理
		String iProductKey = productKey;
		if (!"Xm".equalsIgnoreCase(productKey) && !"GrandShow".equalsIgnoreCase(productKey)) {
			iProductKey = "Complete";
		}
		final String iProductKey1 = iProductKey;
		Optional<PublishStrategy> first = publishStrategyList.stream().filter(e -> e.strategyType().equalsIgnoreCase(iProductKey1)).findFirst();
		PublishStrategy publishStrategy = first.orElseThrow(NullPointerException::new);
		publish = commandService.publish(
				publishStrategy.getScheduleActions(type, json),
				deviceName,
				productKey,
				uuid,
				seqId);
	}
	
	@PostMapping("/client/command/publish2")
	public void Acu35PublishCommands2() {
		String deviceName = "CLT42146028000";
		String productKey = "Xm";
		String uuid = "32e6ef6fce4172f23d4bda0127c30743";
		Long seqId = 0L;
		//加密
		String json1 = "{\"input\":{\"detail\":{\"file1\":[-126,0,32,0,0,0,0,0,0,8,0,0,0,0,0,0,57,-91,34,70,27,58,31,-88,-1,-64,104,50,-87,-97,-17,-77],\"file2\":[-125,0,48,0,0,0,0,0,0,8,0,0,0,0,0,0,-74,-110,98,-42,1,96,91,-124,119,-27,-43,-42,4,-105,-105,-37,-77,-94,-98,-19,-14,-88,82,9,-52,116,-97,4,104,-2,74,122]}}}";
		String type1 = "EncryptInstruction";
		//解密
		String json2 = "{\"input\":{\"detail\":{\"file2\":[-125,0,48,0,0,0,0,0,0,8,0,0,0,0,0,0,-79,6,-30,124,-87,-94,-7,-72,28,-27,53,-126,-120,92,88,115,-64,-124,-67,36,67,83,-28,35,-59,-77,42,5,-108,122,45,-68]}}}";
		String type2 = "DecryptInstruction";
		
		String json = json1;
		String type = type1;
		
		String jsonTime = "{\"input\":{\"time\":{\"currentTime\":{\"dateTime\":{\"date\":{\"year\":2024,\"month\":6,\"day\":13},\"time\":{\"hour\":10,\"minute\":56,\"second\":4,\"nano\":315922000}}},\"timeZoneId\":\"Asia/Shanghai\",\"timezoneOffset\":8.0,\"timeSyncEnabled\":false}}}";
		String typeTime = "SetTimeZoneInstruction";
		synchronized (LockObject.LOCK) {
			PublishStrategy publishStrategy = new Xm();
			while (true) {
				seqId++;
				//固定执行事件
				commandService.publish(
						publishStrategy.getScheduleActions(typeTime, jsonTime),
						deviceName,
						productKey,
						uuid,
						seqId);
				try {
					LockObject.LOCK.wait(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				commandService.publish(
						publishStrategy.getScheduleActions(type, json),
						deviceName,
						productKey,
						uuid,
						seqId);
				if (json.equals(json1)) {
					json = json2;
					type = type2;
				} else {
					json = json1;
					type = type1;
				}
				try {
					log.info("线程锁定");
					LockObject.LOCK.wait(); // 线程A等待，释放锁
				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
				}
				log.info("线程解锁");
			}
		}
		
	}
	
	@PostMapping("/client/command/publish3")
	public void Acu35PublishCommands3() {
		ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
		ZonedDateTime nowInShanghai = ZonedDateTime.now(shanghaiZone);
		
		SetTimeZone setTimeZone = new SetTimeZone();
		SetTimeZone.InputMsg inputMsg = new SetTimeZone.InputMsg();
		TimeData timeData = new TimeData();
		timeData.setTimeZoneId("Asia/Shanghai");
		timeData.setTimeSyncEnabled(true);
		timeData.setTimezoneOffset(8.0F);
		LocalDateTime now = LocalDateTime.now(shanghaiZone);
		Time time = new Time();
		time.setDateTime(now);
		timeData.setCurrentTime(time);
		inputMsg.setTime(timeData);
		setTimeZone.setInput(inputMsg);
		setTimeZone.objectConvertBytes();
		
		String jsonTime = new Gson().toJson(setTimeZone);
		
		String deviceName = "CLT20776562500";
		String productKey = "Xm";
		String uuid = "uuid-uuid-uuid-uuid";

		//加密
		String type1 = "EncryptInstruction";
		
		Encrypt encrypt = new Encrypt();
		Encrypt.InputMsg encryptInputMsg = new Encrypt.InputMsg();
		AuthorizationDetail authorizationDetail = new AuthorizationDetail();
		byte[] bytes1 = null;
		byte[] bytes2 = null;
		{
			bytes1 = new byte[]{-126,
					0,
					32,
					0,
					0,
					0,
					0,
					0,
					0,
					8,
					0,
					0,
					0,
					0,
					0,
					0,
					-46,
					2,
					109,
					97,
					-104,
					-52,
					56,
					91,
					4,
					0,
					64,
					102,
					33,
					107,
					118,
					108};
			bytes2 = new byte[]{-125,
					0,
					48,
					0,
					0,
					0,
					0,
					0,
					0,
					8,
					0,
					0,
					0,
					0,
					0,
					0,
					49,
					114,
					-121,
					-104,
					83,
					-112,
					-92,
					32,
					-33,
					125,
					-122,
					28,
					-119,
					-7,
					-127,
					-79,
					41,
					-76,
					46,
					25,
					-70,
					-117,
					-30,
					-31,
					-8,
					-86,
					106,
					28,
					89,
					44,
					-112,
					-54};
		}
		authorizationDetail.setFile1(bytes1);
		authorizationDetail.setFile2(bytes2);
		encryptInputMsg.setDetail(authorizationDetail);
		encrypt.setInput(encryptInputMsg);
		encrypt.objectConvertBytes();
		
		String json = new Gson().toJson(encrypt);
		String type = type1;
		
		String typeTime = "SetTimeZoneInstruction";
		PublishStrategy publishStrategy = new Xm();
		
		Long seqId = new Random().nextLong();
		//固定执行事件
		commandService.publish(
				publishStrategy.getScheduleActions(typeTime, jsonTime),
				deviceName,
				productKey,
				uuid,seqId
				);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
		seqId = new Random().nextLong();
		commandService.publish(
				publishStrategy.getScheduleActions(type, json),
				deviceName,
				productKey,
				uuid,
				seqId);
		
	}
	
}
